package controllers

import (
	"fox/common"
	"fox/enum/code"
	"fox/enum/fields"
	"fox/models"
	"fox/repositories"
	request2 "fox/request"
	response2 "fox/response"
	"fox/validators"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

type PermissionController struct {
	repository *repositories.PermissionRepository
}

func NewPermissionController() (permissionController *PermissionController) {

	return &PermissionController{repository: repositories.NewPermissionRepository()}

}

func (controller *PermissionController) SavePermission(c *gin.Context) {
	var (
		savePermission *request2.SavePermission
		permission     *models.Permission
		id             int64
		err            error
	)

	savePermission = &request2.SavePermission{}
	if err = c.ShouldBind(&savePermission); err != nil {
		c.JSON(http.StatusOK, response2.Response(code.ParameterError, validators.Translate(err), ""))
		return
	}

	permission = &models.Permission{
		Name:          savePermission.Name,
		Path:          savePermission.Path,
		RequestMethod: savePermission.RequestMethod,
		CreateTime:    common.DateTimeNowFormat(),
		UpdateTime:    common.DateTimeNowFormat(),
		Status:        fields.StatusOk,
	}

	if id, err = controller.repository.InsertPermission(permission); err != nil {
		c.JSON(http.StatusOK, response2.Response(code.QueryError, code.QueryErrorStr, ""))
		return
	}

	c.JSON(http.StatusOK, response2.Response(code.Success, code.SuccessStr, id))
	return
}

func (controller *PermissionController) GetPermission(c *gin.Context) {
	var (
		idStr      string
		idInt      int
		permission *models.Permission
		err        error
	)

	idStr = c.Query("id")
	if idStr == "" {
		c.JSON(http.StatusOK, response2.Response(code.ParameterError, code.ParameterErrorStr, ""))
		return
	}
	if idInt, err = strconv.Atoi(idStr); err != nil {
		c.JSON(http.StatusOK, response2.Response(code.ParameterError, err.Error(), ""))
		return
	}
	if permission, err = controller.repository.SelectPermissionById(int64(idInt)); err != nil {
		c.JSON(http.StatusOK, response2.Response(code.QueryError, err.Error(), ""))
		return
	}
	c.JSON(http.StatusOK, response2.Response(code.Success, code.SuccessStr, permission))
	return

}

func (controller *PermissionController) DeletePermission(c *gin.Context) {
	var (
		idStr string
		idInt int
		err   error
	)

	idStr = c.Query("id")
	if idStr == "" {
		c.JSON(http.StatusOK, response2.Response(code.ParameterError, code.ParameterErrorStr, ""))
		return
	}

	if idInt, err = strconv.Atoi(idStr); err != nil {
		c.JSON(http.StatusOK, response2.Response(code.ParameterError, err.Error(), ""))
		return
	}

	if err = controller.repository.DeletePermissionById(int64(idInt)); err != nil {
		c.JSON(http.StatusOK, response2.Response(code.QueryError, err.Error(), ""))
		return
	}

	c.JSON(http.StatusOK, response2.Response(code.Success, "删除成功", ""))
	return
}

func (controller *PermissionController) UpdatePermission(c *gin.Context) {
	var (
		updatePermission *request2.UpdatePermission
		permission       *models.Permission
		permissionNew    *models.Permission
		err              error
	)

	updatePermission = &request2.UpdatePermission{}
	if err = c.ShouldBind(&updatePermission); err != nil {
		c.JSON(http.StatusOK, response2.Response(code.ParameterError, validators.Translate(err), ""))
		return
	}

	permission = &models.Permission{
		ID: updatePermission.ID,
	}

	if updatePermission.Status != fields.StatusOk && updatePermission.Status != fields.StatusNo {
		c.JSON(http.StatusOK, response2.Response(code.ParameterError, "status有误", ""))
		return
	}
	permission.Status = updatePermission.Status

	if permissionNew, err = controller.repository.UpdatePermissionById(permission); err != nil {
		c.JSON(http.StatusOK, response2.Response(code.QueryError, err.Error(), ""))
		return
	}

	c.JSON(http.StatusOK, response2.Response(code.Success, code.SuccessStr, permissionNew))
	return
}

func (controller *PermissionController) GetPermissions(c *gin.Context) {
	var (
		permission  *request2.Permission
		permissions *response2.Result
		ok          bool
		err         error
	)

	permission = &request2.Permission{}
	if permission.Page, err = strconv.Atoi(c.DefaultQuery("page", "1")); err != nil {
		c.JSON(http.StatusOK, response2.Response(code.ParameterError, err.Error(), ""))
		return
	}
	if permission.Size, err = strconv.Atoi(c.DefaultQuery("size", "10")); err != nil {
		c.JSON(http.StatusOK, response2.Response(code.ParameterError, err.Error(), ""))
		return
	}
	permission.Sort = c.DefaultQuery("sort", fields.DESC)
	if permission.Sort != fields.DESC && permission.Sort != fields.ASC {
		c.JSON(http.StatusOK, response2.Response(code.ParameterError, "排序有误", ""))
		return
	}
	permission.SortField = c.DefaultQuery("sortField", fields.CreateTime)
	if ok = request2.ValidatePermissionSortFile(permission.SortField); ok != true {
		c.JSON(http.StatusOK, response2.Response(code.ParameterError, "排序参数有误", ""))
		return
	}

	if permissions, err = controller.repository.SelectPermissions(permission); err != nil {
		c.JSON(http.StatusOK, response2.Response(code.QueryError, err.Error(), ""))
		return
	}
	c.JSON(http.StatusOK, response2.Response(code.Success, code.SuccessStr, permissions))
	return
}
